package com.tandil.seo;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import utils.AlarmReceiver;
import utils.Constantes;
import utils.PersistenceData;

import java.util.Calendar;

public class Confirmation_Screen extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.confirmation_screen);
        Intent intent = getIntent();
        TextView dominio = (TextView)findViewById(R.id.dominio);
        TextView inicio = (TextView)findViewById(R.id.inicio);
        TextView fin = (TextView)findViewById(R.id.fin);
        TextView saldo = (TextView)findViewById(R.id.saldo);
        TextView transaccion = (TextView)findViewById(R.id.transaccion);

        String confirmacion = intent.getStringExtra("CONFIRMACION");

        if (confirmacion.equalsIgnoreCase("TRANSACCION")){
            String horas = PersistenceData.readHoras(Confirmation_Screen.this, Constantes.ARCHIVO_HORAS);
            horas = horas.replaceAll("\u0000", "");
            setAlarmClock(horas);

            dominio.setText(intent.getStringExtra("DOMINIO"));
            dominio.setVisibility(View.VISIBLE);
            inicio.setText(intent.getStringExtra("INICIO"));
            inicio.setVisibility(View.VISIBLE);
            fin.setText(intent.getStringExtra("FIN"));
            fin.setVisibility(View.VISIBLE);
            saldo.setText(intent.getStringExtra("SALDO"));
            saldo.setVisibility(View.VISIBLE);
            transaccion.setText(intent.getStringExtra("TRANSACCION"));
            transaccion.setVisibility(View.VISIBLE);
        }else if (confirmacion.equalsIgnoreCase("TARJETA")){
                dominio.setText("Tarjeta Registrada");
                inicio.setText("Saldo: " + intent.getStringExtra("SALDO"));
                fin.setVisibility(View.GONE);
                saldo.setVisibility(View.GONE);
                transaccion.setVisibility(View.GONE);
                }




        Button aceptar = (Button)findViewById(R.id.aceptar_cs);
        aceptar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Confirmation_Screen.this, MainActivity.class);
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
                finish();
            }
        });


    }

    private void setAlarmClock(String horas){
        horas.trim();
        Integer int_horas = Integer.parseInt(horas);

        Toast.makeText(Confirmation_Screen.this, horas, Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(Confirmation_Screen.this, AlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(Confirmation_Screen.this, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.HOUR, int_horas);

        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

    }
}